Process_FileUpload.jsp  
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>  
<%@ page import="java.util.List"%>  
<%@ page import="java.util.Iterator"%>  
<%@ page import="java.io.File"%>  
<%@ page import="org.apache.commons.fileupload.*"%>  
<%@ page import="org.apache.commons.fileupload.disk.*"%>  
<%@ page import="org.apache.commons.fileupload.servlet.*"%>  
<%
// *** Restrict Access To Page: Grant or deny access to this page
String MM_authorizedUsers="146240,151945,152786,153948";
String MM_authFailedURL="../login.jsp";
boolean MM_grantAccess=false;
if (session.getValue("MM_Username") != null && !session.getValue("MM_Username").equals("")) {
  if (false || (session.getValue("MM_UserAuthorization")=="") || 
          (MM_authorizedUsers.indexOf((String)session.getValue("MM_UserAuthorization")) >=0)) {
    MM_grantAccess = true;
  }
}
if (!MM_grantAccess) {
  String MM_qsChar = "?";
  if (MM_authFailedURL.indexOf("?") >= 0) MM_qsChar = "&";
  String MM_referrer = request.getRequestURI();
  if (request.getQueryString() != null) MM_referrer = MM_referrer + "?" + request.getQueryString();
  MM_authFailedURL = MM_authFailedURL + MM_qsChar + "accessdenied=" + java.net.URLEncoder.encode(MM_referrer);
  response.sendRedirect(response.encodeRedirectURL(MM_authFailedURL));
  return;
}
%>
<html>  
<head>  
<title>POWA Store [File Process]</title>  
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
</head>  
  
<body>  
<p>
  <%!	  
	//method to return file extension  
	String getFileExt(String xPath){  
  
			//Find extension  
			  
			int dotindex = 0;	//extension character position  
			dotindex = xPath.lastIndexOf('.');  
			  
			if (dotindex == -1){	// no extension	  
				return "";  
			}  
			  
			int slashindex = 0;	//seperator character position  
			slashindex = Math.max(xPath.lastIndexOf('/'),xPath.lastIndexOf('\\'));  
			  
			if (slashindex == -1){	// no seperator characters in string  
				return xPath.substring(dotindex);  
			}  
			  
			if (dotindex < slashindex){	//check last "." character is not before last seperator  
				return "";  
			}  
			return xPath.substring(dotindex);  
	}  
%>  
  <%  // Check that we have a file upload request  
	boolean isMultipart = FileUpload.isMultipartContent(request);  
	out.println("isMultipart="+isMultipart+"<br>");  
	  
	//Create variables for path, filename and extension  
	String newFilePath = application.getRealPath("/") + "Shidi_Test_Server/Product/productImages";  
	String productName ="";  
	String FileExt = "";
	// Create a factory for disk-based file items  
	FileItemFactory factory = new DiskFileItemFactory();  
	  
	// Create a new file upload handler  
	ServletFileUpload upload = new ServletFileUpload(factory);  
	  
	// Parse the request  
	List /* FileItem */ items = upload.parseRequest(request);  
	  
	// Process the uploaded items  
	Iterator iter = items.iterator();  
	//Form fields   
	while (iter.hasNext()) {  
		FileItem item = (FileItem) iter.next();
		if (item.isFormField()) {  
			String name = item.getFieldName();  
			String value = item.getString();  
			if (name.equals("productName")) {  
				productName = value;  
				out.println("Product Name = " + value + "<br><br>");
			}
		}   
		else {  
			String fieldName = item.getFieldName();  
			String fileName = item.getName();  
			FileExt = getFileExt(fileName);  
			String contentType = item.getContentType();  
			boolean isInMemory = item.isInMemory();  
			long sizeInBytes = item.getSize();  
			out.println(fieldName + "<br>");  
			out.println("fileName= " + fileName + "<br>");  
			out.println("FileExt= " + FileExt + "<br>");  
			out.println("contentType= " + contentType + "<br>");  
			out.println("isInMemory= " + isInMemory + "<br>");  
			out.println("sizeInBytes= " + sizeInBytes + "<br>");  
			if (fileName.equals("") || sizeInBytes==0){  
				out.println("Not a valid file.<br>No upload attempted.<br><br>");  
			} else {  
				File uploadedFile = new File(newFilePath+"/", productName+".gif");  
				try{  
					item.write(uploadedFile);  
					out.println(fileName+" was successfully uploaded as "+ productName+FileExt +".<br><br>");  
				}  
				catch (java.lang.Exception e) {  
					out.println("Errors prevented the file upload.<br>"+fileName+ " was not uploaded.<br>");  
					out.println("Error message: " + e + "<br>");  
				} 
			}  
		}
	}
%>
</p>
</body>  
</html>  